﻿/*----------------------------------------------------------------
// Copyright (C) 2011 
// 版权所有。 
//
// 文件名：DrawAnimation.cs
// 文件功能描述：绘制动画
//
// 
// 创建标识：陈奕昆 2011-6-14
//
// 修改标识：
// 修改描述：
//
// 修改标识：
// 修改描述：
//----------------------------------------------------------------*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Drawing2D;
using KHMI.Attributes;

namespace DrawTools
{
    [Serializable]
    public class DrawAnimation : DrawRectangle
    {
        // 子图集合 都是组合图形
        private List<DrawLinkedList> animationPic = new List<DrawLinkedList>();
        private int playID = 0;
        private bool isView = false;

        [ExtendCategoryAttribute("动画设置", VectorName = "组合图元集合")]
        public List<DrawLinkedList> AnimationPic
        {
            get { return animationPic; }
            set { animationPic = value; }
        }

        public DrawAnimation() : this(0, 0, 1, 1)
		{
		}

        public DrawAnimation(int x, int y, int width, int height)
            : base()
        {
            Rectangle = new Rectangle(x, y, width, height);
            Initialize();

#if DEBUG
            DrawLinkedList drawLinked = new DrawLinkedList(100, 100, 100, 100);
            drawLinked.PicPath = @"E:\kun\公司资料\亚太电效\KKScada\DrawTools\bin\Debug\Emf\测试图元\22.k";
            drawLinked.read();
            animationPic.Add(drawLinked);

            DrawLinkedList drawLinked2 = new DrawLinkedList(100, 100, 100, 100);
            drawLinked2.PicPath = @"E:\kun\公司资料\亚太电效\KKScada\DrawTools\bin\Debug\Emf\测试图元\23.k";
            drawLinked2.read();
            animationPic.Add(drawLinked2);
#endif
        }

        /// <summary>
        /// Clone this instance
        /// </summary>
        public override DrawObject Clone(DrawObject drawObject)
        {
            base.Clone(drawObject);
            return this;
        }

        // 重载属性保存方法
        public override void SaveToStream(System.Runtime.Serialization.SerializationInfo info, 
            int orderNumber)
        {
            info.AddValue("AnimationPicCount" + orderNumber.ToString(), AnimationPic.Count);
            for (int i = 0; i < AnimationPic.Count; i++)
            {
                info.AddValue("AnimationPicPath" + i.ToString() + orderNumber.ToString()
                    , AnimationPic[i].PicPath);
            }
            base.SaveToStream(info, orderNumber);
        }

        public override void LoadFromStream(System.Runtime.Serialization.SerializationInfo info, 
            int orderNumber)
        {
            int animationPicCount = info.GetInt32("AnimationPicCount" + orderNumber.ToString());

            if (animationPicCount>0)
            {
                AnimationPic.Clear();
                for (int i = 0; i < animationPicCount; i++)
                {
                    string picPath = info.GetString("AnimationPicPath" + i.ToString()
                        + orderNumber.ToString());
                    DrawLinkedList drawLinked = new DrawLinkedList();
                    drawLinked.PicPath = picPath;
                    drawLinked.read();
                    AnimationPic.Add(drawLinked);
                }
            }
            
            base.LoadFromStream(info, orderNumber);
        }

        public override void Draw(Graphics g)
        {
            try
            {
                Pen pen = new Pen(Color.Blue, PenWidth);
                if (animationPic.Count > 0)
                {
                    if (base.IsView)
                    {
                        animationPic[playID].Rectangle = this.Rectangle;
                        animationPic[playID].Draw(g);

                        if (base.IsView)
                        {
                            playID++;
                            if (playID >= animationPic.Count)
                            {
                                playID = 0;
                            }
                        }
                    }
                    else
                    {
                        animationPic[0].Rectangle = this.Rectangle;
                        animationPic[0].Draw(g);
                    }
                }

                pen.Dispose();
            }
            catch (System.Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            
        }
    }
}
